//
//  GuestCommendView.m
//  X-Touch2
//
//  Created by shengchao yang on 12-3-15.
//  Copyright (c) 2012年 foxhis. All rights reserved.
//

#import "GuestCommendView.h"
#import "Singleton.h"
#import "GridViewItemGuest.h"
#import "FileManager.h"
#import "Common.h"
@implementation GuestCommendView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

#pragma mark - UIGridViewDataSource
-(NSInteger) gridViewRows:(UIGridView *)gridView{
    return 2;
}
-(NSInteger) gridViewCols:(UIGridView *)gridView{
    return 4;
}
-(NSInteger) gridViewTotalCount:(UIGridView *)gridView{
    return [AppSession.commendPluPlus count];
}
-(GridViewItem *) gridView:(UIGridView *)gridView1 itemAtIndex:(NSInteger)index{
    NSString *CellIdentifier = @"CellIdentifier1";
    GridViewItemGuest *item = (GridViewItemGuest *)[gridView1 dequeueReusableCellWithIdentifier:CellIdentifier];
    if (item == nil){
        item = [[GridViewItemGuest alloc] init];
    }
    
//    GridViewItemGuest *item = [[GridViewItemGuest alloc] init];
    PluPlu *tempPlu = (PluPlu *)[AppSession.commendPluPlus objectAtIndex:index];
    [item setData:tempPlu];
    return item;
}
-(BOOL) gridViewPageControlEnable:(UIGridView *)gridView{  //是否使用pageControl控件
    return YES;
}
-(BOOL) gridViewDirection:(UIGridView *)gridView{          //滑动的方向(YES-横向默认  NO-纵向)
    return YES;
}
-(BOOL) gridViewShowScrollIndicator:(UIGridView *)gridView{//是否显示滑动条Indicator(默认NO)
    return NO;
}
#pragma mark - UIGridViewDelegate
-(void) gridView:(UIGridView *)gridView didSelectItemAtIndex:(NSInteger)index{
    MyNSLog(@"弹出全屏窗口 index=%d", index);
    AppSession.commendIndex = index;
    AppSession.fullScreenType = 0;
    postN(SysMsg_RootView_PopOutPluPlu);
}

- (IBAction)buttonClicked:(id)sender {
    if (AppSession.isAnimating) {
        MyNSLog(@"上一个动画还未完成...");
        return;
    }
    [AppSession playMusic:VoiceFile(@"clicked.caf")];
    NSInteger bid = [sender tag];
//    UIButton *btn = (UIButton *)[self viewWithTag:bid];
    if (bid == TagOfUIButton) {
        MyNSLog(@"开始点菜");
        postN(@"SysMsg_GuestNineModeView_refreshGuestNineModeView");
        postN(SysMsg_GuestOrdering_removeCommendIntoNineMode);
    }
}
//刷新界面
-(void) refreshCommendView{
    [((UIButton *)[self viewWithTag:TagOfUIButton]) setTitle:TranslateStr(@"开始点菜") forState:UIControlStateNormal];
    [gridView reloadData];    
    //刷新我的点单总数
    postN(@"SysMsg_GuestOrdering_refreshToolBar");
}

- (void)drawRect:(CGRect)rect{
    addNObserver(@selector(refreshCommendView), @"SysMsg_GuestCommendView_refreshCommendView");
    [((UIButton *)[self viewWithTag:TagOfUIButton]) setTitle:TranslateStr(@"开始点菜") forState:UIControlStateNormal];
    
    [self setFrame:CGRectMake(0, 53, 1024, 715)];
    //用自定义的九宫格view替换占位用的普通view，同时移除普通view
    UIView *tempView = (UIView *)[self viewWithTag:TagOfUIView];
    CGRect frame = tempView.frame;
    [tempView removeFromSuperview];
    
    //刷新我的点单总数
    postN(@"SysMsg_GuestOrdering_refreshToolBar");
    
    gridView = [[UIGridView alloc] initWithTarget:self withFrame:frame withTag:TagOfUIGridView];
    [self addSubview:gridView];
}
-(void)dealloc{
    MyNSLog(@"GuestCommendView is deallocing");
    removeNObserver(@"SysMsg_GuestCommendView_refreshCommendView");
    [gridView release];
    gridView = nil;
    [super dealloc];
}
@end
